package cn.itcast.tanhua.sso.controller;

import cn.itcast.tanhua.sso.service.UserInfoService;
import cn.itcast.tanhua.sso.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @ClassName UserInfoController
 * @Description 完善用户信息
 * @Author WU
 * @Date 2021/5/25 20:55
 * @Version 1.0
 */
@RestController
@RequestMapping("user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;


    /**
    * @Description: 完善用户基本信息
    * @Param: 
    * @return: 
    * @Author: Mr.Tan
    * @Date: 2021/5/25
    */
    @PostMapping("loginReginfo")
    public ResponseEntity<Object> saveUserInfo(@RequestBody Map<String, String> param,@RequestHeader("Authorization") String token){
        try {
            Boolean bool = this.userInfoService.saveUserInfo(param, token);
//        如果返回值为true则添加成功，返回OK的结果类
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
//        否则失败
        ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("保存用户信息失败！").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }


    /**
    * @Description: 完善用户图片信息
    * @Param:
    * @return:
    * @Author: Mr.Tan
    * @Date: 2021/5/25
    */
    @PostMapping("loginReginfo/head")
    public ResponseEntity<Object> saveUserLogo(@RequestParam("headPhoto") MultipartFile file,
                                               @RequestHeader("Authorization") String token) {
        Boolean bool = this.userInfoService.saveUserLogo(file, token);
        if (bool) {
            return ResponseEntity.ok(null);
        }

        //        否则失败
        ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("保存用户logo失败！").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}
